package vip.lsjscl.flowboot.util.converter;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;
import vip.lsjscl.flowboot.util.constants.ActivityStatus;
import vip.lsjscl.flowboot.util.constants.TransitionStatus;

/**
 * 变迁状态转换器
 *
 * @author zhangxingju
 * @date 2025/03/25
 */
@Converter(autoApply = true)
public class TransitionStatusConverter implements AttributeConverter<TransitionStatus, Integer> {
    @Override
    public Integer convertToDatabaseColumn(TransitionStatus status) {
        return status == null ? null : status.getCode();
    }

    @Override
    public TransitionStatus convertToEntityAttribute(Integer code) {
        if (code == null) {
            return null;
        }
        try {
            return TransitionStatus.fromCode(code);
        } catch (IllegalArgumentException e) {
            // 记录错误日志或返回默认值
            return TransitionStatus.NOT_ACTIVATED;
        }
    }
}